<template>
    <div class="app-container">
        <!--查询表单-->
        <el-form :inline="true" class="demo-form-inline">
                            <el-form-item label="itemTypeId">
                    <el-input v-model="searchObj.itemTypeId" placeholder="itemTypeId" />
                </el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="fetchData()">
                查询
            </el-button>
            <el-button type="default" @click="resetData()">清空</el-button>
        </el-form>
        <!-- 列表 -->
        <el-table :data="list" border stripe>
            <el-table-column label="#" width="50">
                <template slot-scope="scope">
                    {{ (page - 1) * limit + scope.$index + 1 }}
                </template>
            </el-table-column>
                <el-table-column prop="itemTypeId" label="itemTypeId" />
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <router-link
                            :to="'/ItemType/edit/' + scope.row.id"
                            style="margin-right:5px;"
                    >
                        <el-button type="primary" size="mini" icon="el-icon-edit">
                            修改
                        </el-button>
                    </router-link>
                    <el-button
                            type="danger"
                            size="mini"
                            icon="el-icon-delete"
                            @click="removeById(scope.row.id)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
                :current-page="page"
                :total="total"
                :page-size="limit"
                :page-sizes="[10,15,20]"
                style="padding: 30px 0; "
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="changePageSize"
                @current-change="changeCurrentPage"
        />
    </div>
</template>
<script>
    import itemTypeApi from '@/api/ItemType'
    export default {
        data() {
            return {
                list: null, // 数据列表
                total: 0, // 数据库中的总记录数
                page: 1, // 默认页码
                limit: 10, // 每页记录数
                searchObj: {} // 查询条件
            }
        },
        created() {
            // 当页面加载时获取数据
            this.fetchData()
        },
        methods: {
            fetchData() {
                itemTypeApi
                    .getPageList(this.page, this.limit, this.searchObj)
                    .then(response => {
                        this.list = response.data.pageModel.list
                        this.total = response.data.pageModel.total
                    })
            },
            // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
            changePageSize(size) {
                this.limit = size
                this.fetchData()
            },
            // 改变页码，page：回调参数，表示当前选中的“页码”
            changeCurrentPage(page) {
                this.page = page
                this.fetchData()
            },
            // 重置表单
            resetData() {
                this.searchObj = {}
                this.fetchData()
            },
            removeById(id) {
                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                    .then(() => {
                        itemTypeApi.removeById(id).then(response => {
                            this.$message.success(response.message)
                            this.fetchData()
                        })
                    })
                    .catch(error => {
                        this.$message.info('取消删除')
                    })
            }
        }
    }
</script>
